package com.app.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.app.R;
import com.app.activity.ActivityWalletDetail;
import com.app.constant.KeyValue;
import com.app.model.bean.WalletDetail;
import com.app.utils.TimeUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/8/3.
 */
public class ActivityWalletDetailListRecyclerViewAdapter extends RecyclerView.Adapter {



    private Context mContext;

    private List<WalletDetail> items = new ArrayList<WalletDetail>();


    public ActivityWalletDetailListRecyclerViewAdapter(Context context) {
        mContext = context;
    }

    public void setItems(List<WalletDetail> items) {
        if (items != null) {
            this.items = items;
            notifyDataSetChanged();
        }
    }

    public void addItems(List<WalletDetail> items) {
        if (items != null && items.size() > 0) {
            items.addAll(items);
            notifyDataSetChanged();
        }
    }


    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = null;
        itemView = LayoutInflater.from(mContext).inflate(R.layout.item_wallet_detail, parent, false);
        MViewHolder viewHolder = new MViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        WalletDetail detail = items.get(position);
        MViewHolder viewHolder = (MViewHolder)holder;
        switch (detail.getType()) {
            case WalletDetail.TYPE_IN:
                viewHolder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.headColorEnd));
                viewHolder.tvMoney.setText("+" + detail.getMoney());
                break;
            case WalletDetail.TYPE_OUT:
                viewHolder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.light_black));
//                viewHolder.tvMoney.setTextColor(Color.parseColor("#222222"));
                viewHolder.tvMoney.setText("-" + detail.getMoney());
                break;
            default:
                break;
        }
        viewHolder.tvTime.setText(TimeUtil.getFormatTime(detail.getTime(), TimeUtil.FORMAT_TIME_SECONDS));
        viewHolder.tvMark.setText(detail.getMark());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, ActivityWalletDetail.class);
                intent.putExtra(KeyValue.DATA, items.get(position));
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return items.size();
    }


    class MViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tvMark)
        TextView tvMark;
        @BindView(R.id.tvTime)
        TextView tvTime;
        @BindView(R.id.tvMoney)
        TextView tvMoney;

        MViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
